import {
  deleteDeviceModel,
  getDeviceModelAll,
  getDeviceModel,
  getDeviceModelId,
  putDeviceModel,
  putDeviceModelSort
} from '@/base/api/DeviceModelApi';
import { Company } from '@/base/model/Company';
import {
  deleteDeviceSort,
  postDeviceSort,
  putDeviceSort,
  getDeviceSort,
  putDeviceSortSort
} from '@/base/api/DeviceSortApi';
import { getDevice } from '@/base/api/DevcieApi';
import { Page } from '@/base/Page';
import { getCompanyList, getCompany } from '@/base/api/CompanyApi';
import { deepAssign } from '@/base/util/Objects';
import { DevVo } from '@/base/model/DevVo';
import { DeviceModel } from '@/base/model/DeviceModel';
import { DeviceSort } from '@/base/model/DeviceSort';

export class DevicesBloc {
  device: any = [];
  currentIdx: number = 0;
  sortDevice = new DeviceSort();
  model: any = {
    id: 0 || undefined,
    companyId: [],
    isFirmware: true,
    isSelfMotion: true,
    isSoftware: true,
    modelIcon: '',
    modelName: '',
    configure: '',
    sortId: 0,
    poster: '',
    createTime: '',
    updateTime: ''
  };
  modelPut: any = {
    id: 0 || undefined,
    companyId: 0 || undefined,
    isFirmware: true,
    isSelfMotion: true,
    isSoftware: true,
    modelIcon: '',
    modelName: '',
    configure: '',
    sortId: 0 || undefined,
    poster: '',
    createTime: '',
    updateTime: ''
  };
  sortPage: Page<DeviceSort> = new Page<DeviceSort>([], 0, 1, 10, 0, []);
  company: Page<Company> = new Page<Company>([], 0, 1, 10, 0, []);
  modelPage: Page<DeviceModel> = new Page<DeviceModel>([], 0, 1, 15, 0, []);
  devices: Page<DevVo> = new Page<DevVo>([], 0, 1, 10, 0, []);
  loading = false;
  sortList: any = [];
  num: number = 0;
  companyList: any = [];
  fileList:any = [];
  fileList1:any = [];
  load(model) {
    this.loading = true;
    let that = this;
    this.num++;
    return getDeviceModelAll({ companyId: model.companyId }).then((res) => {
      this.device = res.data;
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

  getCompanyList(pageIndex: number,
    pageSize: number,
    sortDirection: string) {
    return getCompanyList({ pageIndex, pageSize, sortDirection, name: '', createBegin: '' })
      .then((res) => {
        deepAssign(this.company, res.data)
        this.companyList = res.data.items.map(item => {
          let obj = { companyId: 0, companyName: '' }
          obj.companyId = item.id;
          obj.companyName = item.name
          return obj
        })
        return res;
      }).finally(() => {
        this.loading = false;
      });
  }

  getCompanyName(companyId: number) {
    const item = this.companyList.find(x => x.companyId === companyId);
    return item == null ? '' : item.companyName;
  }

  addSort(sortName: string, sortIcon:string) {
    this.loading = true;
    return postDeviceSort({sortName, sortIcon}).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

  editSort(id: number, sortName: string, sortIcon) {
    this.loading = true;
    return putDeviceSort({id, sortName, sortIcon}).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

  deleteSort(id: number) {
    this.loading = true;
    return deleteDeviceSort({ id }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

  putDeviceSortSort(id: []) {
    this.loading = true;
    return putDeviceSortSort({ id }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

  putDeviceModelSort(row, type) {
    // this.getDeviceModel(1,100,'ASC',this.model.sortId,this.model.companyId).then(res => {
    //
    // })
    let modelIdS = this.modelPage.items.map(item => {
      return item.id
    })
    let index = modelIdS.findIndex(modelId => {
      return modelId === row.id
    })
    if (type === 'down') {
      if (index < modelIdS.length - 1) {
        let modelId = modelIdS[index]
        modelIdS[index] = modelIdS[index + 1]
        modelIdS[index + 1] = modelId
      } else {
        let modelId = modelIdS[index]
        modelIdS[index] = modelIdS[0]
        modelIdS[0] = modelId
      }
    }
    if (type === 'up') {
      if (index > 0) {
        let modelId = modelIdS[index]
        modelIdS[index] = modelIdS[index - 1]
        modelIdS[index - 1] = modelId
      } else {
        let modelId = modelIdS[index]
        modelIdS[index] = modelIdS[modelIdS.length - 1]
        modelIdS[modelIdS.length - 1] = modelId
      }
    }
    this.model.sortId = row.sortId
    return putDeviceModelSort({ id: modelIdS }).then(res => {
      this.getDeviceModel(1, 100, 'ASC', this.model.sortId, this.model.companyId, this.model.modelName)
      return res
    })
  }

  getDeviceModel(pageIndex: number, pageSize: number, sortDirection: string, sortId, companyId, modelName) {
    this.loading = true;
    return getDeviceModel({
      pageIndex,
      pageSize,
      sortId,
      sortDirection,
      companyId,
      id: this.model.model,
      modelName: modelName
    }).then((res) => {
      deepAssign(this.modelPage, res.data);
      return res;
    }).finally(() => {
      this.loading = false;
    })
  }

  getAllSort(pageIndex: number, pageSize: number, sortDirection: string) {
    this.loading = true;
    let that = this;
    return getDeviceSort({
      pageIndex,
      pageSize,
      sortDirection
    }).then((res) => {
      that.sortPage = res.data
      this.sortList = res.data.items.map(function (item) {
        var obj = { id: 0, sortName: '' };
        // @ts-ignore
        obj.id = item.id;
        obj.sortName = item.sortName;
        return obj;
      });
      // if (this.sortDevice.id === 0) {
      //   this.sortDevice = this.sortList[0]
      // }
      return this.sortList;
    }).finally(() => {
      this.loading = false;
    });
  }

  getsortName(sortId: number) {
    const item = this.sortList.find(x => x.id === sortId);
    return item == null ? '' : item.sortName;
  }

  switchStatus1(id, value, index) {
    this.getModel(id)
      .then(res => {
        this.modelPut.isFirmware = !!value;
        this.editModel(this.modelPut)
      })
  }

  switchStatus2(id, value, index) {
    this.getModel(id)
      .then(res => {
        this.modelPut.isSelfMotion = !!value;
        this.editModel(this.modelPut)
      })
  }

  switchStatus3(id, value, index) {
    this.getModel(id)
      .then(res => {
        this.modelPut.isSoftware = !!value;
        this.editModel(this.modelPut)
      })
  }

  getModel(id: number) {
    this.loading = true;
    return getDeviceModelId({ id }).then((res) => {
      deepAssign(this.modelPut, res.data);
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

  editModel(modelPut: any) {
    return putDeviceModel({
      id: modelPut.id,
      companyId: modelPut.companyId,
      channel:modelPut.channel,
      internalModel:modelPut.internalModel,
      isFirmware: modelPut.isFirmware,
      isSelfMotion: modelPut.isSelfMotion,
      isSoftware: modelPut.isSoftware,
      modelIcon: modelPut.modelIcon,
      modelName: modelPut.modelName,
      sortId: modelPut.sortId,
      poster: modelPut.poster,
      configure: modelPut.configure,
      bletype:modelPut.bletype
    }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

  deleteModel(id) {
    return deleteDeviceModel({ id }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

  getDevice(
    pageIndex: number, pageSize: number, sortDirection: string, model: any) {
    this.loading = true;
    return getDevice({
      pageIndex: pageIndex,
      pageSize: pageSize,
      sortDirection: sortDirection,
      agentId: this.model.agentId,
      cityId: this.model.cityId,
      companyId: this.model.companyId,
      createBegin: this.model.createBegin,
      createEnd: this.model.createEnd,
      deviceSn: this.model.deviceSn,
      modelId: this.model.modelId,
      modelName: this.model.modelName,
      provinceId: this.model.provinceId,
      sortId: this.model.sortId,
      statusCode: this.model.statusCode
    }).then((res) => {
      // console.log(res);
      this.devices = res.data;
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

}
